/*
 * InputDataParser.h
 *
 *  Created on: 12.10.2010
 *      Author: jilly
 */

#ifndef INPUTDATAPARSER_H_
#define INPUTDATAPARSER_H_
#define SYMB_EOI -1
#define SYMB_HASH '#'
#define SYMB_AT '@'
#define SYMB_Q 'Q'
#define SYMB_B 'B'
#define SYMB_X 'X'
#define SYMB_SPACE ' '
#define SYMB_LF '\n'
#define SYMB_CR '\r'
#define SYMB_TAB '\t'
#define SYMB_VTAB '\v'
#define SYMB_FF '\f'

#define STATE_A 0
#define STATE_B 1
#define STATE_C 2
#define STATE_D 3
#define STATE_E 4
#define STATE_F 5
#define STATE_G 6
#define STATE_H 7
#define STATE_I 8
#define STATE_J 9
#define STATE_K 10
#define STATE_L 11
#define STATE_M 12

#define DEFAULT_DATA_FILENAME "board.txt"

#include <stdio.h>
#include <string>
#include "../objects/Board.h"

using namespace std;

class InputDataParser {
private:
	/**
	 * file handler
	 */
	FILE* file;
	/**
	 * name of file to parse
	 */
	const char* filename;
	/**
	 * stores all yet read character on current line
	 * (for error output)
	 */
	string lineBuffer;
	/**
	 * on what line we are
	 */
	unsigned int line;
	/**
	 * number of characters on line (excluding tabs)
	 */
	unsigned int chars;
	/**
	 * number of tabs on line
	 */
	unsigned int tabs;
	/**
	 * represents board created during parsing
	 */
	Board* board;
	/**
	 * initializes necessary things
	 */
	void init();
	/**
	 * closes file if it's open and clears allocated memory
	 */
	void terminate();
	/**
	 * reads symbol from input file (one character)
	 */
	char nextSymbol();
	/**
	 * writes error message that symbol 'symb' is unexpected
	 *
	 * throws ParseException
	 */
	void invalidSymbol(char symb);
	/**
	 * returns string containing 'countSpaces' spaces
	 * and 'countTabs' tabs
	 */
	string indent(unsigned int countSpaces, unsigned int countTabs);
	/**
	 * deallocates board
	 */
	void dealloc();
public:
	InputDataParser(const char* filename);
	~InputDataParser();
	/**
	 * parses input file. path is provided via 'filename' property
	 * returns Board object where pieces are placed as well as queen
	 * and bishop
	 * throws ParseException if something goes wrong
	 */
	Board* parse();
};

#endif /* INPUTDATAPARSER_H_ */
